home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
winview
/
procinfo.bas
next >
Wrap
BASIC Source File
|
1999-09-15
|
7KB
|
179 lines
Attribute VB_Name = "modProcInfoWin95"
Option Explicit
Private Declare Function GetModuleHandle Lib "kernel32.dll" _
Alias "GetModuleHandleA" _
(ByVal lpModuleName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32.dll" _
(ByVal hModule As Long, _
ByVal lpProcName As String) As Long
Private Const MAX_PATH As Long = 260
Private Type PROCESSENTRY32
dwSize As Long ' Size in bytes of type
cntUsage As Long ' Number of references to the process
th32ProcessID As Long ' Identifier of the Win32 process
th32DefaultHeapID As Long ' Identifier of the default heap of the process
th32ModuleID As Long ' Module identifier of the process
cntThreads As Long ' Number of execution threads started by the process
th32ParentProcessID As Long ' Identifier of the process that created the process being examined
pcPriClassBase As Long ' Base priority of any threads created by this process
dwflags As Long ' Reserved, do not use
szExeFile As String * MAX_PATH ' Path and filename of the executable file for the process
End Type
Private Const MAX_MODULE_NAME32 As Long = 255
Private Const MAX_MODULE_NAME32_1 As Long = MAX_MODULE_NAME32 + 1
Private Type MODULEENTRY32
dwSize As Long ' Size in bytes of type
th32ModuleID As Long ' Module identifier in the context of the owning process
th32ProcessID As Long ' Identifier of the Win32 process being examined
GlblcntUsage As Long ' Globale usage count on the module
ProccntUsage As Long ' Module usage count in the contect of the owning process
modBaseAddr As Long ' Base address of the module in the contect of the owning process
modBaseSize As Long ' size in bytes of the module
hModule As Long ' Handle of the module in the contect of the owning process
szModule As String * MAX_MODULE_NAME32_1 ' String containing the module name
szExePath As String * MAX_PATH ' String containing the location (path) of the module
End Type
Private Const TH32CS_SNAPHEAPLIST As Long = &H1&
Private Const TH32CS_SNAPPROCESS As Long = &H2&
Private Const TH32CS_SNAPTHREAD As Long = &H4&
Private Const TH32CS_SNAPMODULE As Long = &H8&
Private Const TH32CS_SNAPALL As Long = TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or _
TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE
Private Const TH32CS_SNAPINHERIT As Long = &H80000000
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" _
(ByVal dwflags As Long, _
ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32.dll" _
(ByVal hSnapShot As Long, _
lppe As PROCESSENTRY32) As Boolean
Private Declare Function Process32Next Lib "kernel32.dll" _
(ByVal hSnapShot As Long, _
lppe As PROCESSENTRY32) As Boolean
Private Declare Function Module32First Lib "kernel32.dll" _
(ByVal hSnapShot As Long, _
lpme As MODULEENTRY32) As Boolean
Private Declare Function Module32Next Lib "kernel32.dll" _
(ByVal hSnapShot As Long, _
lpme As MODULEENTRY32) As Boolean
Private Declare Sub CopyMemory Lib "kernel32.dll" _
Alias "RtlMoveMemory" _
(xDes As Any, _
xSrc As Any, _
ByVal Bytes As Long)
Private Declare Function CloseHandle Lib "kernel32.dll" _
(ByVal hWnd As Long) As Long
Public Function SupportsToolHelp() As Boolean
Dim hKernel As Long
Dim pProcCreatTH32 As Long
Dim pProcM32First As Long
Dim pProcM32Next As Long
Dim pProcP32First As Long
Dim pProcP32Next As Long
Dim pProcT32First As Long
Dim pProcT32Next As Long
' obtain the module handle of the kernel to retrieve
' addresses of the tool helper functions
hKernel = GetModuleHandle("KERNEL32.DLL")
If hKernel <> 0 Then
pProcCreatTH32 = GetProcAddress(hKernel, "CreateToolhelp32Snapshot")
pProcM32First = GetProcAddress(hKernel, "Module32First")
pProcM32Next = GetProcAddress(hKernel, "Module32Next")
pProcP32First = GetProcAddress(hKernel, "Process32First")
pProcP32Next = GetProcAddress(hKernel, "Process32Next")
pProcT32First = GetProcAddress(hKernel, "Thread32First")
pProcT32Next = GetProcAddress(hKernel, "Thread32Next")
End If
' all address must be non-NULL to be successfull
SupportsToolHelp = CBool(pProcCreatTH32 And _
pProcM32First And pProcM32Next And _
pProcP32First And pProcP32Next And _
pProcT32First And pProcT32Next)
End Function
Private Function GetProcessModule(ByVal vlPid As Long, _
ByVal vlModuleID As Long, _
ByRef rtME As MODULEENTRY32) As Boolean
Dim hModuleSnap As Long
Dim tME As MODULEENTRY32
' take a snapshot of all modules in the specified process
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, vlPid)
If hModuleSnap = -1 Then Exit Function
' fill the size of the type before using it
tME.dwSize = Len(tME)
' walk to module list of the process and find the module of
' interest. Copy the information to the buffer pointed to
' by rtME so that that it can be returned to the caller.
If Module32First(hModuleSnap, tME) <> False Then
Do
If tME.th32ModuleID = vlModuleID Then
CopyMemory rtME, tME, Len(rtME)
GetProcessModule = True
Exit Do
End If
Loop While Module32Next(hModuleSnap, tME) <> False
End If
CloseHandle hModuleSnap
End Function
Private Function GetProcessInfo(ByVal vlPid As Long, _
ByRef rtPE As PROCESSENTRY32, _
ByRef rtME As MODULEENTRY32) As Long
Dim hProcSnap As Long
Dim tPE As PROCESSENTRY32
hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, vlPid)
If hProcSnap = -1 Then Exit Function
tPE.dwSize = Len(tPE)
If Process32First(hProcSnap, tPE) <> False Then
Do
If tPE.th32ProcessID = vlPid Then
CopyMemory rtPE, tPE, Len(rtPE)
If GetProcessModule(vlPid, tPE.th32ModuleID, rtME) Then
GetProcessInfo = 2
Exit Do
End If
GetProcessInfo = 1
Exit Do
End If
Loop While Process32Next(hProcSnap, tPE) <> False
End If
CloseHandle hProcSnap
End Function
Public Function GetWin95ModuleName(ByVal vlPid As Long) As String
Dim tPE As PROCESSENTRY32
Dim tME As MODULEENTRY32
Select Case GetProcessInfo(vlPid, tPE, tME)
Case 2
GetWin95ModuleName = LPSTRToStr(tME.szExePath)
Case 1
GetWin95ModuleName = LPSTRToStr(tPE.szExeFile)
End Select
End Function